package com.example.converter;

import com.example.message.MessageSourceService;
import com.fasterxml.jackson.core.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.support.NullValue;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;


public class JsonMessageConverter extends MappingJackson2HttpMessageConverter {
    @Value("${code_key:code}")
    private String codeKey;

    @Value("${message_key:message}")
    private String messageKey;

    @Value("${data_key:data}")
    private String data;


    @Autowired
    private MessageSourceService messageSourceService;

//    @Autowired
//    private MessageSourceService sourceService;


//    @Override
//    protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
//        super.writePrefix(generator, object);
//    }
//
//    @Override
//    protected void writeSuffix(JsonGenerator generator, Object object) throws IOException {
//        super.writeSuffix(generator, object);
//    }

    @Override
    protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        if (type.getTypeName().contains("springfox")||req.getRequestURI().startsWith("/private/")) {
            super.writeInternal(object, null, outputMessage);
        } else if (object instanceof Map && ((Map) object).containsKey(codeKey)) {
            outputMessage((Map) object, outputMessage);
        } else {
            Map<String, Object> returnValue = new HashMap<>();
            if (!(object instanceof NullValue)) {
                returnValue.put(data, object);
            }
            returnValue.put(codeKey, "0000");
            returnValue.put(messageKey, "操作成功");
            outputMessage(returnValue, outputMessage);
        }
    }

    private void outputMessage(Map map, HttpOutputMessage outputMessage) throws IOException {
        try {
            if (StringUtils.isEmpty(messageKey)) {//不然如果抛出异常的话，里面的占位符不起作用
                String message = messageSourceService.getMessage(map.get(codeKey).toString());
                map.put(messageKey, message);
            }
        } catch (Exception ignored) {

        } finally {
            super.writeInternal(map, null, outputMessage);
        }
    }
}